package com.mmlc.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.ExpendClassifyAdapter;
import com.mmlc.network.responsebean.CategoryListResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/12.
 */
public class ExpendClassify extends LinearLayout implements View.OnClickListener {
    protected MyGridView grid_expend;

    public ExpendClassify(Context context) {
        this(context, null);
        initView();
    }

    public ExpendClassify(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    Drawable drawable;
    String classifyName, logoUrl;
    DisplayMetrics metrics;

    public ExpendClassify(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ExpendClassify, defStyleAttr, defStyleAttr);
        drawable = array.getDrawable(R.styleable.ExpendClassify_src);
        classifyName = array.getString(R.styleable.ExpendClassify_name);
        array.recycle();

        metrics = getResources().getDisplayMetrics();

        initView();
    }

    View view;
    ImageView iv_classify;
    TextView tv_classify;
    RelativeLayout view_left, view_right;

    private void initView() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.expend_classify, null);

        view_left = ViewUtils.getView(view, R.id.view_left);
        view_right = ViewUtils.getView(view, R.id.view_right);
        iv_classify = ViewUtils.getView(view, R.id.iv_classify);
        tv_classify = ViewUtils.getView(view, R.id.tv_classify);
//        tv_classify2 = ViewUtils.getView(view, R.id.tv_classify2);
        grid_expend = ViewUtils.getView(view, R.id.grid_expend);

        view_right.setLayoutParams(new LinearLayout.LayoutParams(792 * metrics.widthPixels / 1080, LinearLayout.LayoutParams.MATCH_PARENT));
//        tv_classify.setLayoutParams(new RelativeLayout.LayoutParams(288 * metrics.widthPixels / 1080, RelativeLayout.LayoutParams.MATCH_PARENT));
        tv_classify.setWidth(metrics.widthPixels);
        tv_classify.setGravity(Gravity.CENTER);
//        tv_classify2.setWidth(288 * metrics.widthPixels / 1080);
//        tv_classify2.setGravity(Gravity.CENTER);
//        tv_classify.setOnClickListener(this);

        if (drawable != null)
            iv_classify.setImageDrawable(drawable);
        if (!ViewUtils.isEmpty(classifyName)) {
            tv_classify.setText(classifyName);
//            tv_classify2.setText(classifyName);
        }

        datas = new ArrayList<>();
        adapter = new ExpendClassifyAdapter(getContext(), datas);
        grid_expend.setAdapter(adapter);

        view_left.setOnClickListener(this);
        view_right.setOnClickListener(this);

        addView(view);
    }

    public void setDatas(String classifyName, String logoUrl, List<CategoryListResponseBean.DataBean.ListBean.ListCBean> datas, GridView.OnItemClickListener itemClickListener) {
        this.classifyName = classifyName;
        this.logoUrl = logoUrl;

        if (!ViewUtils.isEmpty(this.classifyName)) {
            tv_classify.setText(this.classifyName);
//            tv_classify2.setText(this.classifyName);
        }
        if (!ViewUtils.isEmpty(this.logoUrl)) XUtils.image(iv_classify, this.logoUrl,
                XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_CROP).build());
        if (datas != null) {
            this.datas.clear();
            this.datas.addAll(datas);
            adapter.notifyDataSetChanged();
        }
        if (grid_expend != null) {
            grid_expend.setOnItemClickListener(itemClickListener);
        }
    }

    List<CategoryListResponseBean.DataBean.ListBean.ListCBean> datas;
    ExpendClassifyAdapter adapter;

    /**
     * 展开状态
     */
    boolean isExtend = false;

    public boolean isExtend() {
        return isExtend;
    }

    public void setExtend(boolean extend) {
        isExtend = extend;
    }

    /**
     * 手动展开或收缩
     */
    public void toExpend(boolean toExtend) {
        if (toExtend) {
            extend(true);
        } else {
            shrink(true);
        }
    }

    /**
     * 手动展开或收缩
     */
    public void toExpend(boolean toExtend, boolean isAnimation) {
        if (toExtend) {
            extend(isAnimation);
        } else {
            shrink(isAnimation);
        }
    }

    ObjectAnimator animator_left;

    private void extend(boolean isAnimation) {
        if (animator_left != null) {
            animator_left.cancel();
            animator_left = null;
        }
        animator_left = ObjectAnimator.ofFloat(view_left, "translationX", isAnimation ? 0 : -792 * metrics.widthPixels / 1080, -792 * metrics.widthPixels / 1080);
        animator_left.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                tv_classify2.setVisibility(VISIBLE);
                tv_classify.setWidth(288 * metrics.widthPixels / 1080);

                if (extendListener != null)
                    extendListener.extendEnd();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                tv_classify.setWidth(metrics.widthPixels);
//                tv_classify2.setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator_left.start();
        ObjectAnimator animator_right = ObjectAnimator.ofFloat(view_right, "translationX", isAnimation ? 0 : -792 * metrics.widthPixels / 1080, -792 * metrics.widthPixels / 1080);
        animator_right.start();

        if (extendListener != null) {
            extendListener.extend(this);
        }

        isExtend = true;
    }

    private void shrink(boolean isAnimation) {
//        tv_classify2.setVisibility(GONE);
        tv_classify.setWidth(metrics.widthPixels);

        if (animator_left != null) {
            animator_left.cancel();
            animator_left = null;
        }
        animator_left = ObjectAnimator.ofFloat(view_left, "translationX", isAnimation ? -792 * metrics.widthPixels / 1080 : 0, 0);
        animator_left.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
//                tv_classify2.setVisibility(GONE);
                tv_classify.setWidth(metrics.widthPixels);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (extendListener != null)
                    extendListener.shrinkEnd();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator_left.start();
        ObjectAnimator animator_right = ObjectAnimator.ofFloat(view_right, "translationX", isAnimation ? -792 * metrics.widthPixels / 1080 : 0, 0);
        animator_right.start();

        if (extendListener != null) {
            extendListener.shrink(this);
        }

        isExtend = false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_left:
            case R.id.tv_classify:
//            case R.id.view_right:
                if (isExtend) {
                    shrink(true);
                } else {
                    extend(true);
                }
                break;
        }

        invalidate();
    }

    OnExpendListener extendListener;

    public void setOnExtendListener(OnExpendListener extendListener) {
        this.extendListener = extendListener;
    }

    public interface OnExpendListener {
        /**
         * 展开
         */
        void extend(ExpendClassify classify);

        void extendEnd();

        /**
         * 收缩
         */
        void shrink(ExpendClassify classify);

        void shrinkEnd();
    }
}
